﻿Public Class ucEditAddress
	Inherits System.Web.UI.UserControl

	Public Property OfficerInfo As Entity.OfficerInfoItem
	Public Event AfterUpdate(ByVal officerId As String)

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

	End Sub

	Public Sub initControlt()
		Try
			LoadProvince2()
			LoadData()
		Catch
			Me.lblError.Text = "ไม่สามารถแสดงข้อมูลเพื่อทำการแก้ไขได้ กรุณาทดลองแก้ไขข้อมูลอีกครั้ง"
			Me.lblError.Visible = True
		End Try
	End Sub

	Private Sub LoadData()
		With Me.OfficerInfo
			'############################### Application 2 ##############################################
			Me.OFFICER_MAIL_ADDRESS_NO.Text = .officer_mail_address_no
			Me.OFFICER_MAIL_ADDRESS_MOO.Text = .officer_mail_address_moo
			Me.OFFICER_MAIL_ADDRESS_BUILDING.Text = .officer_mail_address_building
			Me.OFFICER_MAIL_ADDRESS_SOI.Text = .officer_mail_address_soi
			Me.OFFICER_MAIL_ADDRESS_STREET.Text = .officer_mail_address_street
			Common.Utility.SelectChoiceByText(OFFICER_MAIL_ADDRESS_PROVINCE, .officer_mail_address_province)
			Me.LoadDistinct2()
			Common.Utility.SelectChoiceByText(OFFICER_MAIL_ADDRESS_DISTRICT, .officer_mail_address_district)
			Me.LoadSubDistinct2()
			Common.Utility.SelectChoiceByText(OFFICER_MAIL_ADDRESS_SUB_DISTRICT, .officer_mail_address_sub)
			Me.LoadPostCode2()
			Common.Utility.SelectChoiceByText(OFFICER_MAIL_ADDRESS_POSTCODE, .officer_mail_address_postcode)
			'############################### Application 2 ##############################################
		End With
	End Sub

	Private Sub LoadProvince2()
		OFFICER_MAIL_ADDRESS_PROVINCE.DataTextField = "PROVINCE_NAME"
		OFFICER_MAIL_ADDRESS_PROVINCE.DataValueField = "PROVINCE_ID"
		OFFICER_MAIL_ADDRESS_PROVINCE.DataSource = BL.Province.GetAll
		OFFICER_MAIL_ADDRESS_PROVINCE.DataBind()
		OFFICER_MAIL_ADDRESS_PROVINCE.Items.Insert(0, New ListItem("--กรุณาระบุจังหวัด--", "0"))
	End Sub

	Private Sub LoadDistinct2()
		If Not Me.OFFICER_MAIL_ADDRESS_PROVINCE.SelectedValue.Equals("0") Then
			OFFICER_MAIL_ADDRESS_DISTRICT.DataTextField = "DISTRICT_NAME"
			OFFICER_MAIL_ADDRESS_DISTRICT.DataValueField = "DISTRICT_ID"
			OFFICER_MAIL_ADDRESS_DISTRICT.DataSource = BL.District.GetAll(Me.OFFICER_MAIL_ADDRESS_PROVINCE.SelectedValue)
			OFFICER_MAIL_ADDRESS_DISTRICT.DataBind()
		Else
			OFFICER_MAIL_ADDRESS_DISTRICT.Items.Clear()
		End If
		OFFICER_MAIL_ADDRESS_DISTRICT.Items.Insert(0, New ListItem("--กรุณาระบุเขต/อำเภอ--", "0"))
	End Sub

	Private Sub LoadSubDistinct2()
		If Not Me.OFFICER_MAIL_ADDRESS_DISTRICT.SelectedValue.Equals("0") Then
			OFFICER_MAIL_ADDRESS_SUB_DISTRICT.DataTextField = "SUB_DISTRICT_NAME"
			OFFICER_MAIL_ADDRESS_SUB_DISTRICT.DataValueField = "SUB_DISTRICT_ID"
			OFFICER_MAIL_ADDRESS_SUB_DISTRICT.DataSource = BL.SubDistrict.AllSubDistrict(Me.OFFICER_MAIL_ADDRESS_PROVINCE.SelectedValue, Me.OFFICER_MAIL_ADDRESS_DISTRICT.SelectedValue)
			OFFICER_MAIL_ADDRESS_SUB_DISTRICT.DataBind()
		Else
			OFFICER_MAIL_ADDRESS_SUB_DISTRICT.Items.Clear()
		End If
		OFFICER_MAIL_ADDRESS_SUB_DISTRICT.Items.Insert(0, New ListItem("--กรุณาระบุแขวง/ตำบล--", "0"))
	End Sub

	Private Sub LoadPostCode2()
		If Not Me.OFFICER_MAIL_ADDRESS_SUB_DISTRICT.SelectedValue.Equals("0") Then
			OFFICER_MAIL_ADDRESS_POSTCODE.DataTextField = "POSTCODE"
			OFFICER_MAIL_ADDRESS_POSTCODE.DataValueField = "POSTCODE"
			OFFICER_MAIL_ADDRESS_POSTCODE.DataSource = BL.PostCode.AllPostCode(Me.OFFICER_MAIL_ADDRESS_PROVINCE.SelectedValue, Me.OFFICER_MAIL_ADDRESS_DISTRICT.SelectedValue)
			OFFICER_MAIL_ADDRESS_POSTCODE.DataBind()
		Else
			OFFICER_MAIL_ADDRESS_POSTCODE.Items.Clear()
		End If
		OFFICER_MAIL_ADDRESS_POSTCODE.Items.Insert(0, New ListItem("--รหัสไปรษณีย์--", "0"))
	End Sub

	Protected Sub OFFICER_MAIL_ADDRESS_PROVINCE_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles OFFICER_MAIL_ADDRESS_PROVINCE.SelectedIndexChanged
		Me.LoadDistinct2()
		Me.LoadSubDistinct2()
		Me.LoadPostCode2()
	End Sub

	Protected Sub OFFICER_MAIL_ADDRESS_DISTRICT_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles OFFICER_MAIL_ADDRESS_DISTRICT.SelectedIndexChanged
		Me.LoadSubDistinct2()
		Me.LoadPostCode2()
	End Sub

	Protected Sub OFFICER_MAIL_ADDRESS_SUB_DISTRICT_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles OFFICER_MAIL_ADDRESS_SUB_DISTRICT.SelectedIndexChanged
		Me.LoadPostCode2()
	End Sub

	Protected Sub btnOk_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnOk.Click
		Dim addressInfo As New Entity.OfficerInfoItem
		With addressInfo
			.officer_mail_address_no = Me.OFFICER_MAIL_ADDRESS_NO.Text
			.officer_mail_address_moo = Me.OFFICER_MAIL_ADDRESS_MOO.Text
			.officer_mail_address_building = Me.OFFICER_MAIL_ADDRESS_BUILDING.Text
			.officer_mail_address_soi = Me.OFFICER_MAIL_ADDRESS_SOI.Text
			.officer_mail_address_street = Me.OFFICER_MAIL_ADDRESS_STREET.Text
			.officer_mail_address_province = Me.OFFICER_MAIL_ADDRESS_PROVINCE.SelectedItem.Text
			.officer_mail_address_district = Me.OFFICER_MAIL_ADDRESS_DISTRICT.SelectedItem.Text
			.officer_mail_address_sub = Me.OFFICER_MAIL_ADDRESS_SUB_DISTRICT.SelectedItem.Text
			.officer_mail_address_postcode = Me.OFFICER_MAIL_ADDRESS_POSTCODE.SelectedItem.Text
			.upd_date = Now
			.upd_user_id = GlobalApp.Authen.offcier_id
			.officer_id = GlobalApp.Authen.offcier_id
			.officer_birthdate = Nothing
			.bank_id = Nothing
		End With

		If BL.Officer.UpdateOfficer(addressInfo) Then
			RaiseEvent AfterUpdate(GlobalApp.Authen.offcier_id)
		Else
			Me.lblError.Text = "ไม่สามารถแสดงข้อมูลเพื่อทำการแก้ไขได้ กรุณาทดลองแก้ไขข้อมูลอีกครั้ง"
			Me.lblError.Visible = True
		End If
	End Sub
End Class